package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Author: hsl
 * @CreateTime: 2025-03-10  13:42
 */
@Slf4j
@Service("1001")
public class AlbumStrategy implements ItemTypeStrategy {

    @Resource
    private UserPaidAlbumMapper userPaidAlbumMapper;
    @Override
    public int savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        // 声明一个变量  进行返回结果赋值
        int result = 0;

        // 1.  根据itemType类型进行判断看是买的啥   专辑 声音 还是 vip

        if (SystemConstant.ORDER_ITEM_TYPE_ALBUM.equals(userPaidRecordVo.getItemType())) {
            //  买的是专辑
            //  查看用户是否购买过此专辑
            LambdaQueryWrapper<UserPaidAlbum> userPaidAlbumLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userPaidAlbumLambdaQueryWrapper.eq(UserPaidAlbum::getUserId, userPaidRecordVo.getUserId())
                    .eq(UserPaidAlbum::getAlbumId, userPaidRecordVo.getItemIdList().get(0));
            UserPaidAlbum userPaidAlbum = userPaidAlbumMapper.selectOne(userPaidAlbumLambdaQueryWrapper);
            if (null == userPaidAlbum) {
                // 用户没有购买过此专辑
                //  创建一个新的对象 进行赋值 且插入数据库即可
                UserPaidAlbum userPaidAlbum1 = new UserPaidAlbum();
                userPaidAlbum1.setUserId(userPaidRecordVo.getUserId());
                userPaidAlbum1.setAlbumId(userPaidRecordVo.getItemIdList().get(0));
                userPaidAlbum1.setOrderNo(userPaidRecordVo.getOrderNo());
                return result += userPaidAlbumMapper.insert(userPaidAlbum1);
            }
            return result;
        }
        return result;
    }
}